package com.youdianstar.app.common.util;

import android.text.InputFilter;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.widget.EditText;

/**
 * File: EditUtil
 * Author: 82149 Create: 2021/11/18 14:52
 * Changes (from 2021/11/18)
 * --------------------------------------------------
 * description:
 * ---------------------------------------------------
 */
public class EditUtil {

    /**
     * 设置光标
     *
     * @param editText EditText输入框
     */
    public static void setEditConsole(EditText editText){
        String content = editText.getText().toString();
        if (!TextUtils.isEmpty(content)){
            editText.setSelection(content.length());
        }
    }

    /**
     * 替换EditText 多行模式下 空格符 ->不换行空格
     *
     * @param editText EditText输入框
     */
    public static void setEditSpaceFilter(EditText editText) {
        InputFilter filter = new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                boolean isSpanned = source instanceof Spanned;
                if (isSpanned){
                    return TextUtils.replace(source,new String[]{" ","\u3000"},new CharSequence[]{"\u00A0","\u00A0"});
                }else {
                    return source.toString().replaceAll(" ","\u00A0").replaceAll("\\u3000","\u00A0");
                }
            }
        };
        editText.setFilters(new InputFilter[]{filter,new InputFilter.LengthFilter(300)});
    }


    /**
     * 限制输入小数点后2位
     * --> 值当输入类型是 浮点型
     * @param editText EditText输入框
     */
    public static void setEditDecimalFilter(EditText editText,int digits,int max){
        InputFilter filter = new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                int len = end - start;
                //以点开始的时候，自动在前面添加0
                if(source.toString().equals(".") && dstart == 0){
                    return "0.";
                }
                //如果起始位置为0,且第二位跟的不是".",则无法后续输入
                if(!source.toString().equals(".") && dest.toString().equals("0")){
                    return "";
                }
                int dlen = dest.length();
                for (int i = 0; i < dstart; i++) {
                    if (dest.charAt(i) == '.') {
                        return (dlen-(i+1) + len > digits) ? "" : new SpannableStringBuilder(source, start, end);
                    }
                }
                for (int i = start; i < end; ++i) {
                    if (source.charAt(i) == '.') {
                        if ((dlen-dend) + (end-(i + 1)) > digits)
                            return "";
                        else
                            break;
                    }
                }
                return new SpannableStringBuilder(source, start, end);
            }
        };
        editText.setFilters(new InputFilter[]{filter,new InputFilter.LengthFilter(max)});
    }

    /**
     * 限制输入浮点型 大小
     * --> 只当输入类型是 浮点型
     * -->默认最大输入9位数
     * @param editText EditText输入框
     */
    public static void setEditDecimalMinMax(EditText editText,float min,float max){
        InputFilter filter = new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                try {
                    //以点开始的时候，自动在前面添加0
                    if(source.toString().equals(".") && dstart == 0){
                        return "0.";
                    }
                    if (dest.toString().contains(".")) {
                        int index = dest.toString().indexOf(".");
                        int mlength = dest.toString().substring(index).length();
                        if (mlength == 3) {
                            return "";
                        }
                    }
                    //限制大小
                    float input = Float.parseFloat(dest + source.toString());
                    if (isInRange(min, max, input))
                        return null;
                } catch (Exception ignored){}
                return "";
            }
        };
        editText.setFilters(new InputFilter[]{filter,new InputFilter.LengthFilter(9)});
    }

    private static boolean isInRange(float a, float b, float c) {
        return b > a ? c >= a && c <= b : c >= b && c <= a;
    }
}
